package com.ruoyi.experiment.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.domain.ExperimentsType;
import com.ruoyi.experiment.domain.Project;
import com.ruoyi.experiment.domain.Sample;
import com.ruoyi.experiment.model.EProject;
import com.ruoyi.experiment.model.ExperimentFlume;
import com.ruoyi.experiment.service.IExperimentService;
import com.ruoyi.finance.model.Ids;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/experiment/experiment")
public class ExperimentController extends BaseController {

    @Autowired
    IExperimentService experimentService;
    @PostMapping("/getNoFlumeExperiments")
    public AjaxResult getNoFlumeExperiments(@RequestBody Ids ids){
        List<Long> experimentsIds = new ArrayList<>();
        for(Long id : ids.getIds()){
            experimentsIds.addAll(experimentService.selectNoFlumeExperiments(id));
        }
        return new AjaxResult(200,"cg",experimentsIds);
    }
    @PostMapping("/getNoFlumeProjects")
    public AjaxResult getNoFlumeProjects(@RequestBody Ids ids){
        List<Long> experimentsIds = new ArrayList<>();
        for(Long id : ids.getIds()){
            experimentsIds.addAll(experimentService.selectNoFlumeProjects(id));
        }
        return new AjaxResult(200,"cg",experimentsIds);
    }
//    @PreAuthorize("@ss.hasPermi('experiment:experiment')")
    @GetMapping("/item/list")
    public TableDataInfo getAllItem(Project project){
        startPage();
        List<EProject> eProjects = experimentService.selectAllItem(project);
        return getDataTable(eProjects);
    }

//    @PreAuthorize("@ss.hasPermi('experiment:experiment')")
    @GetMapping("/sample")
    public TableDataInfo getSamplesByItem(Sample sample){
        startPage();
        List<Sample> samples = experimentService.selectSamples(sample);
        return getDataTable(samples);
    }
    @PostMapping("/sample/export")
    public void export(HttpServletResponse response, Sample sample)
    {
        List<Sample> list = experimentService.selectSamples(sample);
        ExcelUtil<Sample> util = new ExcelUtil<Sample>(Sample.class);
        util.exportExcel(response, list, "实验类型管理数据");
    }

    /**
     * 请求流转样品
     * @param sample
     * @return
     */
    @PostMapping("/sample/call")
    public AjaxResult callForSample(@RequestBody List<Sample> sample){
        experimentService.callForSample(sample);
        return success();
    }

    /**
     * 流转
     */
    @PostMapping("/flume")
    public AjaxResult flume(@RequestBody ExperimentFlume flume){
        boolean f = experimentService.flume(flume);
        return AjaxResult.success();
    }

    /**
     * 查询实验列表
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Experiment experimentalManagement)
    {
        startPage();
        List<Experiment> list = experimentService.selectExperimentalManagementList(experimentalManagement);
        return getDataTable(list);
    }

    /**
     * 导出实验列表
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:export')")
    @Log(title = "实验管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Experiment experimentalManagement)
    {
        List<Experiment> list = experimentService.selectExperimentalManagementList(experimentalManagement);
        ExcelUtil<Experiment> util = new ExcelUtil<Experiment>(Experiment.class);
        util.exportExcel(response, list, "实验管理数据");
    }

    /**
     * 获取实验详细信息
     */
    @GetMapping(value = "/{sampleId}")
    public AjaxResult getInfo(@PathVariable("sampleId") Long sampleId)
    {
        return success(experimentService.selectExperimentBySampleId(sampleId));
    }

    /**
     * 新增实验
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:add')")
    @Log(title = "实验管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Experiment experiment)
    {
        experiment.setExpermentType(1);
        if (experiment.getSampleIds()!=null && experiment.getSampleIds().size()>1){
            for (Long sampleId : experiment.getSampleIds()) {
                experiment.setSampleId(sampleId);
                experimentService.insertExperimentalManagement(experiment);
            }
            return AjaxResult.success();
        }else
            return toAjax(experimentService.insertExperimentalManagement(experiment));
    }

    /**
     * 修改实验管理
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:edit')")
    @Log(title = "实验管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Experiment experimentalManagement)
    {
        return toAjax(experimentService.updateExperimentalManagement(experimentalManagement));
    }

    /**
     * 删除实验管理
     */
//    @PreAuthorize("@ss.hasPermi('experiment:experiment:remove')")
    @Log(title = "实验管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sampleId}")
    public AjaxResult remove(@PathVariable("sampleId") Long sampleId)
    {
        return toAjax(experimentService.deleteExperimentalManagementBySampleId(sampleId));
    }

    @GetMapping("/samplebyid")
    public AjaxResult getSample(Long id){
        Sample sample = experimentService.selectSampleById(id);
        return success(sample);
    }

}
